#!/bin/bash
#在/opt/目录下创建“ip.txt”文本，并放入需要测试的ip以作后续测试

a=`cat /opt/ip.txt`

#在/opt目录下创建这三个txt文本
cd /opt
>./result.txt
>./result1.txt
>./result2.txt

#for循环，ping“ip.txt”内的ip，一共5次，间隔0.5s，结果输入至“result.log”
for i in $a
	do
	ping -c 5 -i 0.5 $i >./result.log

#首先，总体判断一下有无丢包情况
loss=`cat ./result.log | grep loss | grep -v grep | awk -F , '{print $3}' | awk -F % '{print $1}' | sed s/[[:space:]]//g`
	if [ "$loss" -ge 1 ];then
		echo $i >>./result1.txt
		continue
	fi

#如果没有丢包，则判断延时情况
cat ./result.log | grep from | grep -v grep | while read line
	do
	#获取ping命令的延时
	delay=`echo $line | awk -F = '{print $4}' | awk '{print $1}'`
	if [ `echo "$delay>50 " | bc` -eq 1 ];then
		echo $line >>result2.txt
		break
	fi
	done
done

#合并文件，输出丢包及时延结果
echo "丢包的主机:" >>./result.txt
cat result1.txt >>./result.txt
echo ""
echo "时延超50ms的主机:" >>result.txt
cat result2.txt >>./result.txt
